<template>
    <n-drawer :show="active" :width="width" @update:show="(flag:boolean) => (active = flag)">
        <n-drawer-content :native-scrollbar="false" title="详情" closable>
            <n-spin :show="loading">
                <n-space vertical :size="32" v-if="log.id">
                    <n-descriptions label-placement="left" title="基本信息" bordered :column="1">
                        <n-descriptions-item :label-style="labelStyle" label="日志ID">
                            <n-text code>{{ log.id }}</n-text>
                        </n-descriptions-item>
                        <n-descriptions-item :label-style="labelStyle" label="日志时间">{{ log.time }}</n-descriptions-item>
                        <n-descriptions-item :label-style="labelStyle" label="请求地">{{ log.ip }} {{ log.city }}</n-descriptions-item>
                        <n-descriptions-item :label-style="labelStyle" label="操作信息">{{ log.memo }}</n-descriptions-item>

                        <n-descriptions-item :label-style="labelStyle" label="操作设备">{{ log.device }}</n-descriptions-item>
                    </n-descriptions>
                    <n-descriptions label-placement="left" title="变动数据" bordered :column="1">
                        <n-descriptions-item :label-style="labelStyle" label="变动前">{{ log.odata }}</n-descriptions-item>
                        <n-descriptions-item :label-style="labelStyle" label="变动后">{{ log.ndata }}</n-descriptions-item>
                    </n-descriptions>
                </n-space>
            </n-spin>
            <template #footer>
                <n-space justify="end">
                    <n-button type="tertiary" @click="() => (active = false)">
                        <template #icon>
                            <n-icon :component="CloseRound" />
                        </template>
                        关闭
                    </n-button>
                </n-space>
            </template>
        </n-drawer-content>
    </n-drawer>
</template>

<script lang="ts" setup>
import { computed, onMounted, ref } from 'vue'
import { CloseRound } from '@vicons/material'
import { useStore } from '@/store'
import { Http } from '@/libs'
import { Filters } from '@/libs'

const store = useStore()
const width = computed(() => Math.max(store.state.browser.innerWidth * 0.4, 1200))
const loading = computed(() => store.state.loading.flag)

const labelStyle = {
    width: '150px',
}

const prop = defineProps({
    show: {
        type: Boolean,
        required: true,
    },
    item: {
        type: Object,
        required: true,
    },
})

const emit = defineEmits(['update:show', 'refresh'])

const active = computed({
    get: () => prop.show,
    set: val => emit('update:show', val),
})

const log = ref<any>({})

const execLoad = () => {
    Http.post('/ops/sys/log/action/detail', { id: prop.item.id }).then(resp => {
        if (resp.stat.err > 0) {
            return
        }
        log.value = resp.data
    })
}

onMounted(() => {
    execLoad()
})
</script>
